package juc.concurrent.forkjoin;

import java.util.concurrent.RecursiveTask;

public class MyRecursiveTask extends RecursiveTask<Integer> {

    private int beginPosition;
    private int endPosition;

    MyRecursiveTask(int beginPosition,int endPosition){
        super();
        this.beginPosition = beginPosition;
        this.endPosition = endPosition;
        System.out.println("#" + (beginPosition + " " + endPosition));
    }
    @Override
    protected Integer compute() {
        System.out.println(Thread.currentThread().getName() +"---------");
        int sumValue = 0;
        System.out.println("compute" + beginPosition + " " + endPosition);
        if(endPosition - beginPosition != 0){
            System.out.println("!=0-");
            int middleNum = (endPosition + beginPosition)/2;
            System.out.println("left传入值" +beginPosition + " " +  middleNum);
            MyRecursiveTask leftTask = new MyRecursiveTask(beginPosition, middleNum);
            System.out.println("right传入值" +(middleNum+1) + " " +  endPosition);
            MyRecursiveTask rightTask = new MyRecursiveTask(middleNum+1, endPosition);
            this.invokeAll(leftTask,rightTask);
            Integer leftValue = leftTask.join();
            Integer rigitValue = rightTask.join();
            return leftValue + rigitValue;
        }else {
            return endPosition;
        }

    }
}
